const api = require('../../api/api.js');
const app = getApp();

Page({
  data: {
    userId: '',
    currentIndex: 0,
    imgUrl: app.globalData.imgUrl,
    liveStockSpecies: [],
    addLiveStock: [],
    feed: [],
    yimiao: [],
    quarantine: [],
    slaughter: [],
    storage: [],
    steps: ['品种', '入圈', '饲料', '疫苗', '检疫', '屠宰', '储存'],
    siteIds: []
  },
  onLoad() {
    let that = this;
    that.setData({
      liveStockSpecies: [],
      addLiveStock: [],
      feed: [],
      yimiao: [],
      quarantine: [],
      slaughter: [],
      storage: []
    })
    wx.getStorage({
      key: 'userId',
      success: function(res) {
        let userId = res.data;
        that.setData({
          userId: res.data
        });
        // that.queryLiveStockSpecies();
        // that.queryAddLiveStock();
        // that.queryFeed();
        // that.queryYiMiao();
        // that.queryQuarantine();
        // that.querySlaughter();
        // that.queryStorage();
      },
    })
    wx.getStorage({
      key: 'role',
      success(res) {
        if (res.data === 1) {
          that.getLSOriginData()
        } else {
          // 获取基地
          api.queryPlantation({
            data: {
              account: that.data.userId
            },
            success(res) {
              let siteIds = res.data.plantation.map(item => {
                return item.siteId
              })
              that.setData({
                siteIds
              })
              that.getLSOriginData_admin(0)
            }
          })
        }
      }
    })
  },
  getLSOriginData() {
    let that = this
    api.getLSOriginData({
      data: {
        account: that.data.userId
      },
      success(res) {
        that.formatData(res)
      }
    })
  },
  getLSOriginData_admin(count) {
    let that = this
    if (count === that.data.siteIds.length) {
      return
    }
    api.getLSOriginData_admin({
      data: {
        siteId: that.data.siteIds[count]
      },
      success(res) {
        that.formatData(res)
        that.getLSOriginData_admin(++count)
      }
    })
  },
  formatData(res) {
    let that = this
    let data = res.data

    let liveStockSpecies = that.data.liveStockSpecies
    data.liveStockSpecies_s.map(i => {
      i.image = that.data.imgUrl + 'liveStockSpeciesImage/' + i.image
    })
    liveStockSpecies.push(...data.liveStockSpecies_s)

    let addLiveStock = that.data.addLiveStock
    data.addLiveStock.map(i => {
      i.image = that.data.imgUrl + 'addLiveStockImage/' + i.image;
    })
    addLiveStock.push(...data.addLiveStock)

    let feed = that.data.feed
    data.feed.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'feedImage/' + i.image;
      })
      feed.push(...item)
    })
    let yimiao = that.data.yimiao
    data.yiMiao.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'yimiaoImage/' + i.image;
      })
      yimiao.push(...item)
    })
    let quarantine = that.data.quarantine
    data.quarantine.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'quarantineImage/' + i.image;
      })
      quarantine.push(...item)
    })
    let slaughter = that.data.slaughter
    data.slaughter.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'slaughterImage/' + i.image;
      })
      slaughter.push(...item)
    })
    let storage = that.data.storage
    data.storage.map(item => {
      item.map(i => {
        i.image = that.data.imgUrl + 'storageImage/' + i.image;
      })
      storage.push(...item)
    })
    that.setData({
      liveStockSpecies,
      addLiveStock,
      feed,
      yimiao,
      quarantine,
      slaughter,
      storage
    })
  },
  swiper(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      currentIndex: Number.parseInt(index)
    });
  },
  change(e) {
    this.setData({
      currentIndex: e.detail.current
    });
  },
  queryLiveStockSpecies() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'liveStockSpeciesImage/';
    let userId = this.data.userId;
    api.queryLiveStockSpecies({
      data: {
        livestockId: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let liveStockSpecies = res.data.LiveStockSpecies;
        liveStockSpecies.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          liveStockSpecies
        });
      }
    });
  },
  queryAddLiveStock() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'addLiveStockImage/';
    let userId = this.data.userId;
    api.queryAddLiveStock({
      data: {
        placeId: -1,
        siteId: -1,
        speciesId: -1,
        id: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let addLiveStock = res.data.AddLiveStock;
        addLiveStock.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          addLiveStock
        });
      }
    });
  },
  queryFeed() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'feedImage/';
    let userId = this.data.userId;
    api.queryFeed({
      data: {
        id: -1,
        addLivestockId: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let feed = res.data.Feed;
        feed.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          feed
        });
      }
    });
  },
  queryYiMiao() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'yimiaoImage/';
    let userId = this.data.userId;
    api.queryYiMiao({
      data: {
        id: -1,
        addLivestockId: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let yimiao = res.data.YiMiao;
        yimiao.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          yimiao
        });
      }
    });
  },
  queryQuarantine() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'quarantineImage/';
    let userId = this.data.userId;
    api.queryQuarantine({
      data: {
        id: -1,
        addLivestockId: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let quarantine = res.data.Quarantine;
        quarantine.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          quarantine
        });
      }
    });
  },
  querySlaughter() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'slaughterImage/';
    let userId = this.data.userId;
    api.querySlaughter({
      data: {
        id: '',
        addLivestockId: '',
        userId
      },
      success(res) {
        console.log(res.data);
        let slaughter = res.data.Slaughter;
        slaughter.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          slaughter
        });
      }
    });
  },
  queryStorage() {
    let that = this;
    let imgUrl = this.data.imgUrl + 'storageImage/';
    let userId = this.data.userId;
    api.queryStorage({
      data: {
        storageId: '',
        produceId: '',
        warehouseId: -1,
        type: 'livestock',
        account: userId
      },
      success(res) {
        console.log(res.data);
        let storage = res.data.Storage;
        storage.map((i) => {
          i.image = imgUrl + i.image;
        });
        that.setData({
          storage
        });
      }
    });
  },
  goToAddFormPage() {
    let currentIndex = this.data.currentIndex;
    wx.navigateTo({
      url: `/pages/livestock-addform/livestock-addform?currentIndex=${currentIndex}`,
    })
  },
  goToDetailPage(e) {
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/info-display/info-display?id=${id}&type=${type}`,
    })
  }
})